options compress=YES;
libname manasi ''; 
libname f831 '';  

* MERGE PARENTAL SSI AND DI APPLICATIONS INTO CHILDRENS' RECORDS;

* USE ONLY HUN AND PAN FROM PARENTS' SSR RECORDS;
data manasi.ssr_parents_hunpan;
	set manasi.ssr_parents_full(keep=hun pan toa mft dob dobyy dod sex);
	if pan='*00000000' then delete;
run;

* ELIMINATE DUPLICATES BY HUN AND PAN AND SORT ;
proc sort data=manasi.ssr_parents_hunpan out=manasi.ssr_parents_hunpan nodupkey;
	by pan hun;
run;

* SORT PARENT 831 RECORDS ;
%macro sort(year);
	proc sort data=f831.par831summ_&year. out=f831.par831summ_&year.;
		by ssn;
	run;
%mend sort;

%sort(1985);
%sort(1986);
%sort(1987);
%sort(1988);
%sort(1989);
%sort(1990);
%sort(1991);
%sort(1992);
%sort(1993);
%sort(1994);
%sort(1995);
%sort(1996);
%sort(1997);
%sort(1998);
%sort(1999);
%sort(2000);
%sort(2001);
%sort(2002);
%sort(2003);
%sort(2004);
%sort(2005);
%sort(2006);
%sort(2007);
%sort(2008);
%sort(2009);
%sort(2010);
%sort(2011);

* MERGE IN PARENT 831 RECORDS ;
data manasi.par_831_hunmerge;
	merge 	
			manasi.ssr_parents_hunpan 
			(rename=(pan=ssn dob=dob_ssr dobyy=dobyy_ssr sex=sex_ssr toa=toa_ssr mft=mft_ssr dod=dod_ssr) 
				in=fromssrx)

			f831.par831summ_1985 (keep=ssn n_par831_: in=frompar831_1985x)
			f831.par831summ_1986 (keep=ssn n_par831_: in=frompar831_1986x)
			f831.par831summ_1987 (keep=ssn n_par831_: in=frompar831_1987x)
			f831.par831summ_1988 (keep=ssn n_par831_: in=frompar831_1988x)
			f831.par831summ_1989 (keep=ssn n_par831_: in=frompar831_1989x)
			f831.par831summ_1990 (keep=ssn n_par831_: in=frompar831_1990x)
			f831.par831summ_1991 (keep=ssn n_par831_: in=frompar831_1991x)
			f831.par831summ_1992 (keep=ssn n_par831_: in=frompar831_1992x)
			f831.par831summ_1993 (keep=ssn n_par831_: in=frompar831_1993x)
			f831.par831summ_1994 (keep=ssn n_par831_: in=frompar831_1994x)
			f831.par831summ_1995 (keep=ssn n_par831_: in=frompar831_1995x)
			f831.par831summ_1996 (keep=ssn n_par831_: in=frompar831_1996x)
			f831.par831summ_1997 (keep=ssn n_par831_: in=frompar831_1997x)
			f831.par831summ_1998 (keep=ssn n_par831_: in=frompar831_1998x)
			f831.par831summ_1999 (keep=ssn n_par831_: in=frompar831_1999x)
			f831.par831summ_2000 (keep=ssn n_par831_: in=frompar831_2000x)
			f831.par831summ_2001 (keep=ssn n_par831_: in=frompar831_2001x)
			f831.par831summ_2002 (keep=ssn n_par831_: in=frompar831_2002x)
			f831.par831summ_2003 (keep=ssn n_par831_: in=frompar831_2003x)
			f831.par831summ_2004 (keep=ssn n_par831_: in=frompar831_2004x)
			f831.par831summ_2005 (keep=ssn n_par831_: in=frompar831_2005x)
			f831.par831summ_2006 (keep=ssn n_par831_: in=frompar831_2006x)
			f831.par831summ_2007 (keep=ssn n_par831_: in=frompar831_2007x)
			f831.par831summ_2008 (keep=ssn n_par831_: in=frompar831_2008x)
			f831.par831summ_2009 (keep=ssn n_par831_: in=frompar831_2009x)
			f831.par831summ_2010 (keep=ssn n_par831_: in=frompar831_2010x)
			f831.par831summ_2011 (keep=ssn n_par831_: in=frompar831_2011x)
			;

	by ssn; 
	fromssr = fromssrx;
	frompar831_1985 = frompar831_1985x;
	frompar831_1986 = frompar831_1986x;
	frompar831_1987 = frompar831_1987x;
	frompar831_1988 = frompar831_1988x;
	frompar831_1989 = frompar831_1989x;
	frompar831_1990 = frompar831_1990x;
	frompar831_1991 = frompar831_1991x;
	frompar831_1992 = frompar831_1992x;
	frompar831_1993 = frompar831_1993x;
	frompar831_1994 = frompar831_1994x;
	frompar831_1995 = frompar831_1995x;
	frompar831_1996 = frompar831_1996x;
	frompar831_1997 = frompar831_1997x;
	frompar831_1998 = frompar831_1998x;
	frompar831_1999 = frompar831_1999x;
	frompar831_2000 = frompar831_2000x;
	frompar831_2001 = frompar831_2001x;
	frompar831_2002 = frompar831_2002x;
	frompar831_2003 = frompar831_2003x;
	frompar831_2004 = frompar831_2004x;
	frompar831_2005 = frompar831_2005x;
	frompar831_2006 = frompar831_2006x;
	frompar831_2007 = frompar831_2007x;
	frompar831_2008 = frompar831_2008x;
	frompar831_2009 = frompar831_2009x;
	frompar831_2010 = frompar831_2010x;
	frompar831_2011 = frompar831_2011x;

run;

%macro tab(year);
	proc tabulate data=manasi.par_831_hunmerge;
		class frompar831_&year. fromssr;
		table frompar831_&year., fromssr;
	run;
%mend tab;

%tab(1985);
%tab(1986);
%tab(1987);
%tab(1988);
%tab(1989);
%tab(1990);
%tab(1991);
%tab(1992);
%tab(1993);
%tab(1994);
%tab(1995);
%tab(1996);
%tab(1997);
%tab(1998);
%tab(1999);
%tab(2000);
%tab(2001);
%tab(2002);
%tab(2003);
%tab(2004);
%tab(2005);
%tab(2006);
%tab(2007);
%tab(2008);
%tab(2009);
%tab(2010);
%tab(2011);

* MERGE IN VARIABLE INDICATING WHETHER PARENT WAS IN HOUSEHOLD AT ANY TIME
BETWEEN 2002 AND 2006 (GENERAL TREATMENT PERIOD);
proc sort data=manasi.par_831_hunmerge out=manasi.par_831_hunmerge;
	by hun ssn;
run;

data manasi.par_831_hunmerge;
	merge 	
			manasi.par_831_hunmerge

			manasi.ssr_parents_estpdonly_wide
			(rename=(pan=ssn) keep=hun pan inhh0206);

	by hun ssn; 
run;
